package com.huawei.esdk.uc.sametime.preferences;

import org.apache.log4j.Logger;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class VideoSubPreferencePage extends FieldEditorPreferencePage implements
        IWorkbenchPreferencePage
{

    private Logger logger = Logger.getLogger(VideoSubPreferencePage.class);

    private ComboFieldEditor camera;

    private String[][] fieldStrings;

    public VideoSubPreferencePage()
    {
        super(GRID);
        setPreferenceStore(EsdkPluginActivator.getDefault()
                .getPreferenceStore());
        setDescription(Messages.VideoSubPreferencePage_DESCRIPTION);
    }

    /**
     * Creates the field editors. Field editors are abstractions of the common
     * GUI blocks needed to manipulate various types of preferences. Each field
     * editor knows how to save and restore itself.
     */
    public void createFieldEditors()
    {

        fieldStrings = UCSettingsService.getCameraDevList();

        camera = new ComboFieldEditor(PreferenceConstants.P_CAMERA,
                Messages.VideoSubPreferencePage_CAMERA, fieldStrings,
                getFieldEditorParent());

        addField(camera);

        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        if (!isUCLogin)
        {
            camera.setEnabled(false, getFieldEditorParent());
        }

        adjustGridLayout();
    }

    /**
     * 提交“确定”或“应用”按钮，覆盖FieldEditorPreferencePage.performOk方法
     */
    public boolean performOk()
    {
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();

        logger.debug("performOk isUCLogin = " + isUCLogin);

        if (!isUCLogin)
        {
            return true;
        }

        IPreferenceStore cameraStore = camera.getPreferenceStore();
        String cameraType = cameraStore.getString(PreferenceConstants.P_CAMERA);

        boolean cameraDev_Res = true;
        logger.debug("fieldStrings.length" + fieldStrings.length);
        logger.debug("cameraType = " + cameraType);
        if (fieldStrings.length > 0)
        {
            logger
                    .debug("setCameraDev index = "
                            + Integer.parseInt(cameraType));
            cameraDev_Res = UCSettingsService.setCameraDev(Integer
                    .parseInt(cameraType));
        }
        if (!cameraDev_Res)
        {
            logger.error("set Camera Devices failed !");
            setErrorMessage(Messages.VideoSubPreferencePage_ERROR_CHANGE_CAMERA_INFO_FAIL);
        }
        else
        {
            logger.debug("super.performOk()");
            super.performOk();
        }
        return cameraDev_Res;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench)
    {
    }

}